﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace StringsAndCultures
{
    class StringTests
    {
    //    public static void EncodeString(string toBeEncoded) 
    //    {
    //        Encoding utf8 = Encoding.UTF8;
    //        Encoding utf16 = Encoding.Unicode;
    //        Encoding ascii = Encoding.ASCII;
    //        Encoding windows1251 = Encoding.GetEncoding("windows-1251");
    //        byte[] array = utf16.GetBytes(toBeEncoded);
    //        Print(array, utf16);
    //        byte[] arrayUTF8 = utf8.GetBytes(toBeEncoded);
    //        Print(arrayUTF8, utf8);
    //        byte[] arrayASCII = ascii.GetBytes(toBeEncoded);
    //        Print(arrayASCII, ascii);
    //        byte[] arraywindows1251 = windows1251.GetBytes(toBeEncoded);
    //        Print(arraywindows1251, windows1251);

    //    }

    //    public static void Print(byte[] array, Encoding encoding) 
    //    {
    //        Console.WriteLine(BitConverter.ToString(array));
    //        Console.WriteLine(encoding.GetString(array));
    //        Console.WriteLine();
    //    }

        //static void PrintBytes(string input)
        //{
        //    Encoding UTF8 = Encoding.UTF8;
        //    byte[] array = UTF8.GetBytes(input);
        //    Console.WriteLine();
        //    foreach (var item in array)
        //    {
        //        Console.Write("\\x{0:x}",item);
        //    }
        //    Console.WriteLine();
        //}

        public static void Decode(string fileName) 
        {
            StreamReader reader = new StreamReader(fileName);
            using (reader) 
            {
                string input = reader.ReadToEnd();
                byte[] array = Convert.FromBase64String(input);
                using (FileStream decoded = new FileStream("Decoded.exe", FileMode.Create)) 
                {
                    decoded.Write(array, 0, array.Length);
                }

            }
        }

        static void Main()
        {
            //Console.Write("napi6i ne6to: ");
            //string str = Console.ReadLine();
            //Console.WriteLine();
            //EncodeString(str);
            FileStream stream = new FileStream("notepad.exe", FileMode.Open);
            using (stream) 
            {
                using (StreamWriter output = new StreamWriter("out.txt"))
                {
                    
                    byte[] buf = new byte[stream.Length];
                    stream.Read(buf, 0, buf.Length);
                    string fileasBase64 = Convert.ToBase64String(buf);
                    int i = 0;
                    foreach (var item in fileasBase64)
                    {
                        output.Write(item);
                        i++;
                        if (i == 40) 
                        {
                            output.Write('\n');
                            i = 0;
                        }
                    }               
                }
            }
            Decode("out.txt");
        }
    }
}
